﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    public class ExplosionManager
    {
        public List<Explosion> explosions = new List<Explosion>();
        Random rnd = new Random();

        public void Update(Timer t)
        {
            for (int i = 0; i < explosions.Count; i++ )
            {
                bool b = explosions[i].Update(t);
                if (b == false)
                    explosions.RemoveAt(i--);
            }
        }

        public void Draw(SpriteBatch sb)
        {
            foreach (Explosion exp in explosions)
            {
                exp.Draw(sb);
            }
        }

        float RndFloat()
        {
            float inv = 1.0f / 65535.0f;
            int r = rnd.Next(65535);
            return ((float)r) * inv; 
        }

        public void AddExplosion(Vector2 pos)
        {
            Explosion exp = new Explosion();

            int particleCount = rnd.Next(32);
            for (int i = 0; i < particleCount; i++)
            {
                Particle p = new Particle();
                p.pos = pos;
                p.vel = new Vector2(RndFloat() * 2 - 1, RndFloat() * 2 - 1) * 100;
                p.vel = new Vector2(RndFloat() * 2 - 1, RndFloat() * 2 - 1) * 500;
                p.acc = new Vector2(0, 0);
                p.life = 0.1f + RndFloat() * 2;
                p.size = RndFloat() * 8;
                p.sizeStep = -p.size / p.life;
                p.color = new Vector4(1, 1, 1, 1);
                p.colorStep = new Vector4(0, 0, 0, -p.life);
                exp.particles.Add(p);
            }

            explosions.Add(exp);
        }

        public void AddExplosionBig(Vector2 pos)
        {
            Explosion exp = new Explosion();

            int particleCount = rnd.Next(32);
            for (int i = 0; i < particleCount; i++)
            {
                Particle p = new Particle();
                p.pos = pos;
                p.vel = new Vector2(RndFloat() * 2 - 1, RndFloat() * 2 - 1) * 100;
                p.vel = new Vector2(RndFloat() * 2 - 1, RndFloat() * 2 - 1) * 500;
                p.acc = new Vector2(0, 0);
                p.life = 0.1f + RndFloat() * 2;
                p.size = 16 + RndFloat() * 32;
                p.sizeStep = -p.size / p.life;
                p.color = new Vector4(1, 1, 1, 1);
                p.colorStep = new Vector4(0, 0, 0, -p.life);
                exp.particles.Add(p);
            }

            explosions.Add(exp);
        }
    }
}
